package com.adee.gulimall.product.app;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.product.entity.CategoryEntity;
import com.adee.gulimall.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;



/**
 * 商品三级分类
 *
 * @author adee
 * @email 609980188@qq.com
 * @date 2022-08-19 16:28:49
 */
@Slf4j
@CacheConfig(cacheNames = "my-redis-cache1", cacheManager = "redisCacheManager")
@Api(tags = "product/category 商品/类别")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 树形结构列表
     */
    //@Cacheable(value = {"category"},key = "'listWithTree'")
    @ApiOperation("查询类别树")
    @GetMapping("/list/tree")
    // @RequiresPermissions("product:category:list")
    public R listWithTree(){
        List<CategoryEntity> list = categoryService.listWithTree();

        return R.ok().put("data", list);
    }


    /**
     * 列表
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("查询详情")
    @GetMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @CacheEvict(value = {"category"},key = "'listWithTree'")
    @ApiOperation("创建一条数据")
    @PostMapping("/save")
    // @RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok().put("catId", category.getCatId());
    }

    /**
     * 修改
     */
    @CacheEvict(value = {"category"},key = "'listWithTree'")
    @ApiOperation("修改一条数据")
    @PostMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateDetails(category);

        return R.ok();
    }

    /**
     * 删除，通过@TableLogic注解到CategoryEntity.showStatus字段，标识逻辑删除字段
     */
    @CacheEvict(value = {"category"},key = "'listWithTree'")
    @ApiOperation("逻辑删除")
    @PostMapping("/delete")
    // @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        boolean b = categoryService.removeByIds(Arrays.asList(catIds));
        if(b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 逻辑删除
     */
    @CacheEvict(value = {"category"},key = "'listWithTree'")
    @PostMapping("/remove")
    public R remove(@RequestBody Long[] catIds) {
        List<CategoryEntity> list = Arrays.stream(catIds).map(id -> {
            CategoryEntity entity = new CategoryEntity();
            entity.setCatId(id);
            entity.setShowStatus(0);
            return entity;
        }).collect(Collectors.toList());
        boolean b = categoryService.updateBatchById(list);
        if(b) {
            return R.ok();
        } else {
            return R.error();
        }

    }


    /**
     * 压测redis集群
     */
    @GetMapping("/test/redis/cluster")
    public R testRedisCluster() {
        int i = ThreadLocalRandom.current().nextInt(10000);
        redisTemplate.opsForValue().set("key"+i, "v"+i);
        Object v2 = redisTemplate.opsForValue().get("key" + i);
        return R.ok().put("data", v2);
    }

    /**
     * 测试客户端post请求是否自动发送options探测请求
     */
    @PostMapping("/test/ops")
    public R testOptions(@RequestBody Map<String, Object> map) {
        log.info("request body: {}", map);
        return R.ok().put("data", map);
    }

    /**
     * 盗取cookie
     */
    @PostMapping("/test/cookie")
    public R testCookie(@RequestBody String cookie) {
        log.info("cookie: {}", cookie);
        return R.ok().put("data", cookie);
    }
}
